package com.echo.code.thread.aqs;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.LockSupport;

/**
 * LockSupportExam Created by baiyap on 2018/10/10.
 */
public class LockSupportExam {

  public static void main(String[] args) {
    Thread t1 = new Thread(() -> {
      System.out.println(Thread.currentThread().getName() + " before park");
      // park 100 seconds
      LockSupport.parkNanos(TimeUnit.NANOSECONDS.convert(100, TimeUnit.SECONDS));
      System.out.println(Thread.currentThread().getName() + " after park");
    }, "t1");
    Thread t2 = new Thread(() -> {
      try {
        TimeUnit.SECONDS.sleep(1);
      } catch (InterruptedException e) {
      }
      System.out.println(Thread.currentThread().getName() + " unPark t1");
      LockSupport.unpark(t1);
    }, "t2");
    Thread t3 = new Thread(() -> {
      System.out.println(Thread.currentThread().getName() + " park 5 seconds");
      //park 5 seconds
      LockSupport.parkUntil(
          System.currentTimeMillis() + TimeUnit.MILLISECONDS.convert(5, TimeUnit.SECONDS));
      System.out.println(Thread.currentThread().getName() + " after park");
    }, "t3");
    t1.start();
    t2.start();
    t3.start();
    try {
      t1.join();
      t2.join();
      t3.join();
    } catch (InterruptedException e) {
    }
  }
}
